//------------------------------------------------------------------------------
// <copyright file="ApplicationExceptionHandler.cs" company="Damian Antonowicz">
//       copyright © 2010 Damian Antonowicz
// </copyright>
// <author>Damian Antonowicz</author>
// <email>poczta@damianantonowicz.pl</email>
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using System.IO;
using System.Collections.Specialized;
using DotBeer.Business.Entities.CustomEventArgs;

namespace DotBeer.Business.Components.Exceptions
{
    [ConfigurationElementType(typeof(CustomHandlerData))]
    public class ApplicationExceptionHandler : IExceptionHandler
    {
        #region private members

        AppTextExceptionFormatter formatter;

        #endregion

        #region properties

        public static event EventHandler<ApplicationExceptionEventArgs> ExceptionOccured;

        #endregion

        #region constructors

        public ApplicationExceptionHandler(NameValueCollection ignore)
        {
            this.formatter = new AppTextExceptionFormatter();
        }

        #endregion

        #region public methods

        #region IExceptionHandler Members

        public Exception HandleException(Exception exception, Guid handlingInstanceId)
        {
            this.OnExceptionOccured(new ApplicationExceptionEventArgs(formatter.Format(exception)));
            
            return exception;
        }

        #endregion

        #endregion

        #region private methods

        private void OnExceptionOccured(ApplicationExceptionEventArgs e)
        {
            if (ExceptionOccured != null)
                ExceptionOccured(this, e);
        }

        #endregion
    }
}
